package com.scs.application.modules.dept.controller;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.scs.application.core.query.QueryContext;
import com.scs.application.core.query.QueryContextHolder;
import com.scs.application.modules.dept.request.OprtUseRequest;
import com.scs.application.modules.sys.model.PageResult;
import com.scs.application.modules.sys.model.RestResult;
import org.springframework.web.bind.annotation.*;

import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.modules.dept.service.OprtUseItemService;
import com.scs.application.modules.dept.entity.OprtUseItem;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 */
@RestController
@RequestMapping("/dept/oprt_use_item")
public class OprtUseItemController extends CrudController<OprtUseItemService, OprtUseItem>{

    /**
     * 科室消耗
     * 添加/更新数据
     */
    @PostMapping("/save_by_stock")
    public RestResult saveByStock(@RequestBody Map<String, String> params) {
        List<String> stockIds = (List<String>) JSONArray.parse(params.get("stockIds"));

        service.saveByStockIds(stockIds, params.get("useId"));
        return RestResult.ok("数据保存成功");
    }

    /**
     * 添加/更新数据 冲红
     */
    @PostMapping("/save_by_work")
    public RestResult saveByWork(@RequestBody Map<String, String> params) {
        List<String> workIds = (List<String>) JSONArray.parse(params.get("stockIds"));

        service.saveByWorkIds(workIds, params.get("useId"));
        return RestResult.ok("数据保存成功");
    }


    /**
     * 添加/更新数据 病人消耗
     */
    @PostMapping("/saveByOprtSn")
    public RestResult saveByOprtSn(@RequestBody OprtUseRequest oprtUseRequest) {
        service.saveByOprtSn(oprtUseRequest);
        return RestResult.ok("操作成功");
    }

    /**
     *  冲红选择耗材列表
     * @return
     */
    @GetMapping("/pageForRedList")
    public PageResult pageForRedList() {
        QueryContext queryContext = QueryContextHolder.currentContext();
        IPage pageResult = service.pageForRedList(queryContext);
        return PageResult.ok("查询成功", pageResult.getRecords(), pageResult.getTotal());
    }
}
